//
//  HLLiveGiftCollectionView.m
//  YueYue
//
//  Created by hulu001 on 2024/3/14.
//

#import "HLLiveGiftCollectionView.h"
#import "HLLiveGiftCollectionLayout.h"
#import "HLLiveGiftCollectionViewCell.h"

@interface HLLiveGiftCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) QMUIEmptyView *emptyView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, assign) NSUInteger pageCount;

@end

@implementation HLLiveGiftCollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:CGRectZero]) {
        [self setupView];
    }
    return self;
}



- (void)setupView
{
    CGFloat itemW = (SCREEN_WIDTH - 54)/4.0;
    CGFloat itemH = itemW *1.15;
    HLLiveGiftCollectionLayout *layout = [[HLLiveGiftCollectionLayout alloc] init];
    layout.itemSize = CGSizeMake(itemW, itemH);
    layout.minimumLineSpacing = 8;
    layout.minimumInteritemSpacing = 8;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    layout.headerReferenceSize = CGSizeMake(0, 0);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.backgroundColor = UIColorBlack;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[HLLiveGiftCollectionViewCell class] forCellWithReuseIdentifier:[HLLiveGiftCollectionViewCell identifyCell]];
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-22);
    }];
    
    self.pageControl = [[UIPageControl alloc] init];
    _pageControl.pageIndicatorTintColor = HLTextColor3;
    _pageControl.currentPageIndicatorTintColor = UIColorWhite;
    _pageControl.enabled = NO;
    [self addSubview:_pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom);
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo(-2);
        make.height.mas_equalTo(20);
    }];
    
    self.emptyView = [[QMUIEmptyView alloc] init];
    [self.emptyView setImage:UIImageMake(@"empty_icon_6")];
    self.emptyView.imageViewInsets = UIEdgeInsetsMake(0, 0, -26, 0);
    [self.emptyView setTextLabelText:@"空空如也"];
    [self.emptyView setDetailTextLabelText:@"哇哦，这是一个礼物也没有"];
    self.emptyView.hidden = YES;
    [self addSubview:self.emptyView];
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(8);
        make.right.mas_equalTo(-8);
        make.bottom.mas_equalTo(-2);
    }];
    
    [self requestGift];
}


- (void)requestGift
{
    HLLiveGiftListRequest *giftListRequest = [[HLLiveGiftListRequest alloc] initWithParams:@{}];
    [giftListRequest startWithCompletionBlockWithSuccess:^(__kindof HLLiveGiftListRequest * _Nonnull request) {
        if (request.isSuccess){
            self.gifts = [request giftList];
        }
    } failure:nil];
}

- (void)setGifts:(NSArray<HLLiveGiftModel *> *)gifts
{
    _gifts = gifts;
    self.emptyView.hidden = (gifts.count > 0);
    self.pageCount = gifts.count;
    while (self.pageCount % 8 != 0) {
        ++self.pageCount;
    }
    self.pageControl.numberOfPages = self.pageCount / 8;
    [self.collectionView reloadData];
    
}

#pragma mark - UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pageCount;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLLiveGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLLiveGiftCollectionViewCell identifyCell] forIndexPath:indexPath];
    
    if (indexPath.item >= self.gifts.count)
    {
        [cell setCellHidden:YES];
    } else {
        cell.model = self.gifts[indexPath.row];
        [cell setCellHidden:NO];
    }

    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item >= self.gifts.count){
        return;
    }
    if (self.changeModel) {
        self.changeModel.isChange = NO;
    }
    HLLiveGiftModel *model = self.gifts[indexPath.row];
    model.isChange = YES;
    self.changeModel = model;
    
    [self.collectionView reloadData];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat doublePage = scrollView.contentOffset.x/SCREEN_WIDTH;
    int intPage = (int)(doublePage +0.5);
    self.pageControl.currentPage = intPage;
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self;
}

@end
